/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import dao.DBManager;
import dao.ServiceManager;
import java.util.Vector;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.IDownloadListener;
import listener.IKeyListener;
import model.ErrorMessage;
import model.Help;
import ui.AListItem;
import ui.List;
import ui.ListItem;
import util.StringUtils;

/**
 *
 * @author tho.hoang
 */
public class HelpForm extends AForm implements ICommandListener, IKeyListener {

    private List list;
    private Vector helps;
    private String dbKey = "";
    private ListItem liAboutUs;

    public HelpForm() {
    }

    public HelpForm(MStockMidlet control, Display display, int branch, int option) {
        super(control, display);
        setOption(branch, option);
    }

    protected void initData() {
        this.lang = new String[]{"Giúp đỡ", "Chọn", "Trở về", "Giới thiệu"};

        this.recycle = control.getRecycle();
        this.icon = control.getIcons()[8];
    }

    protected void initGui() {
        this.list = new List(display, control, lang[0], background, icon);

        this.ok = new Command(lang[1], Command.OK);
        this.back = new Command(lang[2], Command.BACK);

        list.setSelectCommand(ok);
        list.setCancelCommand(back);
        list.setCommandListener(this);
        list.setKeyListener(this);

        loadData();
    }

    protected void loadData() {
        Vector temp = recycle.getHelps();

        if ((temp != null) && (temp.size() > 0)) {
            this.helps = temp;
            showData();

        } else {
            Vector temp2 = null;
//            DBManager.selectAll(DBManager.DB_HELP, dbKey);
            if ((temp2 != null) && (temp2.size() > 0)) {
                String s;
                String[] item;
                Help model;
                Vector temp3 = new Vector();
                for (int i = 0; i < temp2.size(); i++) {
                    s = temp2.elementAt(i).toString();
                    item = StringUtils.splitAdvance(s, "::");
                    model = new Help();
                    model.setId(Integer.parseInt(item[0].trim()));
                    model.setName(item[1].trim());
                    model.setLevels(Integer.parseInt(item[2].trim()));

                    // sort
                    if (temp3.size() == 0) {
                        temp3.addElement(model);
                    } else {
                        int pos = -1;
                        for (int j = temp3.size() - 1; j >= 0; j--) {
                            Help order = (Help) temp3.elementAt(j);
                            if (order.getLevels() > model.getLevels()) {
                                pos = j;
                                break;
                            }
                        }

                        if (pos == -1) {
                            temp3.insertElementAt(model, 0);
                        } else {
                            temp3.addElement(model);
                        }
                    }
                }

                this.helps = temp3;
                recycle.setHelps(temp3);
                showData();

            } else {
                ServiceManager.getHelps(this.control, 2, new IDownloadListener() {

                    public void completedAction(Object result) {
                        if (result != null) {
                            if (result instanceof ErrorMessage) {
                                showError((ErrorMessage) result);
                            } else {
                                Vector temp = (Vector) result;
                                if (temp.size() > 0) {
                                    helps = temp;
                                    recycle.setHelps(temp);
                                    insertDB();
                                    showData();
                                } else {
                                    showError(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }
                            }
                        } else {
                            // ket qua tra ve null
                            showError(new ErrorMessage(ErrorMessage.ERROR_TRANSFER_DATA));
                        }
                    }
                });
            }
        }
    }

    protected void showData() {
        Help help;
        ListItem si;
        String title = "";

        if (helps.size() < 9) {
            this.liAboutUs = new ListItem("liAboutUs", "1. " + lang[3]);
            list.addItem(liAboutUs);

            for (int i = 0; i < helps.size(); i++) {
                help = (Help) helps.elementAt(i);
                title = i + 2 + ". " + StringUtils.convertHTMLtoString(help.getName());
                si = new ListItem(String.valueOf(help.getId()), title);
                list.addItem(si);
            }

        } else {
            this.liAboutUs = new ListItem("liAboutUs", "01. " + lang[3]);
            list.addItem(liAboutUs);

            for (int i = 0; i < helps.size(); i++) {
                help = (Help) helps.elementAt(i);
                if (i < 8) {
                    title = "0";
                } else {
                    title = "";
                }
                title += i + 2 + ". " + StringUtils.convertHTMLtoString(help.getName());
                si = new ListItem(String.valueOf(help.getId()), title);
                list.addItem(si);
            }
        }
        list.show();
    }

    protected void showError(ErrorMessage error) {
        list.setSelectCommand(null);
        list.addItem(new ListItem("-1", error.getMessage()));
        list.show();
    }

    public void commandAction(Command command) {
        if (command == ok) {
            AListItem item = list.getSelectedItem();

            control.getBackManager().push(this.getClass().getName() + ":" + branch + ":" + option);
            recycle.setHelpID(item.getId());
            recycle.setHelpTitle(item.getText().substring(item.getText().indexOf(" ") + 1));

            if (item.getId().equals(liAboutUs.getId())) {
                new HelpDetailForm(control, display, branch, State.OTHER).show();

            } else {
                new HelpDetailForm(control, display, branch, State.NONE).show();
            }
        } else if (command == back) {
            control.back(0);
        }
    }

    private void insertDB() {
        Help help;
        String s;
        Vector temp = new Vector(helps.size());
        for (int i = 0; i < helps.size(); i++) {
            help = (Help) helps.elementAt(i);
            s = help.getId() + "::" + help.getName() + "::" + help.getLevels();
            temp.addElement(s);
        }
        DBManager.insert(DBManager.DB_HELP, temp, dbKey);
    }

    public void keyAction(int key) {
        if (key == List.FIRE) {
            commandAction(ok);
        }
    }
}
